package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageSaleListActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageSaleOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageSaleListBean;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 *
 */

public class RefundStorageSaleListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStorageSaleListBean.SaleBodyBean> mSaleRefundStorageList;
    private LayoutInflater inflater;
    private RefundStorageSaleListActivity.RefundStorageSaleListener mRefundStorageSaleListener;
    private String mSaleNumber, mContractNumber;

    public RefundStorageSaleListAdapter(Context context, List<RefundStorageSaleListBean.SaleBodyBean> saleRefundStorageList, String saleNumber, String contractNumber, RefundStorageSaleListActivity.RefundStorageSaleListener refundStorageSaleListener) {
        mContext = context;
        mSaleRefundStorageList = saleRefundStorageList;
        mRefundStorageSaleListener = refundStorageSaleListener;
        inflater = LayoutInflater.from(context);
        mSaleNumber = saleNumber;
        mContractNumber = contractNumber;
    }

    @Override
    public int getCount() {
        return mSaleRefundStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mSaleRefundStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_refund_storage_sale_list_first_item, null);
            EditText saleNumber = (EditText) convertView.findViewById(R.id.saleNumber);
            EditText contractNumber = (EditText) convertView.findViewById(R.id.contractNumber);
            Button search = (Button) convertView.findViewById(R.id.search);
            saleNumber.setText(mSaleNumber);
            contractNumber.setText(mContractNumber);
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mRefundStorageSaleListener.onNoDoubleClickListener(saleNumber.getText().toString(), contractNumber.getText().toString());
                }
            });
        } else {
            RefundStorageSaleListBean.SaleBodyBean saleRefundStorageItem = mSaleRefundStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_refund_storage_sale_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, saleRefundStorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.saleNumber = (TextView) view.findViewById(R.id.saleNumber);
        viewHolder.contractNumber = (TextView) view.findViewById(R.id.contractNumber);
        viewHolder.customerName = (TextView) view.findViewById(R.id.customerName);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStorageSaleListBean.SaleBodyBean saleRefundStorageItem) {
        viewHolder.saleNumber.setText(saleRefundStorageItem.getRefer_receipt_code());
        viewHolder.contractNumber.setText(saleRefundStorageItem.getPreorder_id());
        viewHolder.customerName.setText(saleRefundStorageItem.getSupplier_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, RefundStorageSaleOverviewActivity.class);
                intent.putExtra(RefundStorageExtra.SALE_NUMBER, viewHolder.saleNumber.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView saleNumber, contractNumber, customerName;
    }
}
